package com.slofzx.nuoda.evaluation.version.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.slofzx.nuoda.evaluation.version.dao.ModelKeyItemRefDAO;
import com.slofzx.nuoda.evaluation.version.dto.ModelKeyItemRefDTO;
import com.slofzx.nuoda.evaluation.version.vo.ModelKeyItemRefVO;
import com.slofzx.nuoda.evaluation.version.entity.ModelKeyItemRef;
import com.slofzx.nuoda.evaluation.version.service.ModelKeyItemRefService;
import org.springframework.beans.factory.annotation.Autowired;
import com.slofzx.nuoda.common.enums.CommonResultEnum;
import com.slofzx.nuoda.common.enums.ServiceResult;
import com.slofzx.nuoda.common.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 评价模型关键指标关联服务实现类
 *
 * @author nuoda
 * @since 2022-10-17 17:15:54
 */
@Service("modelKeyItemRefService")
public class ModelKeyItemRefServiceImpl extends BaseServiceImpl implements ModelKeyItemRefService {
    @Autowired
    private ModelKeyItemRefDAO modelKeyItemRefDAO;

    /**
     * 将数据表数据转换为返回数据类型
     *
     * @param entity 数据表对应类
     * @return 实例对象
     */
    protected ModelKeyItemRefVO parseEntity2VO(ModelKeyItemRef entity) {
        ModelKeyItemRefVO vo = new ModelKeyItemRefVO();
        vo.setId(entity.getId());
        vo.setModelId(entity.getModelId());
        vo.setIrrBeforeTax(entity.getIrrBeforeTax());
        vo.setIrrAfterTax(entity.getIrrAfterTax());
        vo.setPbpBeforeTax(entity.getPbpBeforeTax());
        vo.setPbpAfterTax(entity.getPbpAfterTax());
        vo.setNpvBeforeTax(entity.getNpvBeforeTax());
        vo.setNpvAfterTax(entity.getNpvAfterTax());
        vo.setCutoffYear(entity.getCutoffYear());
        vo.setCreateUser(entity.getCreateUser());
        vo.setCreateTime(entity.getCreateTime());
        return vo;
    }

    /**
     * 将数据传递类转换为数据表数据类
     *
     * @param dto 数据传递类实例
     * @return 实例对象
     */
    protected ModelKeyItemRef parseDTO2Entity(ModelKeyItemRefDTO dto) {
        ModelKeyItemRef entity = new ModelKeyItemRef();
        entity.setId(dto.getId());
        entity.setModelId(dto.getModelId());
        entity.setIrrBeforeTax(dto.getIrrBeforeTax());
        entity.setIrrAfterTax(dto.getIrrAfterTax());
        entity.setPbpBeforeTax(dto.getPbpBeforeTax());
        entity.setPbpAfterTax(dto.getPbpAfterTax());
        entity.setNpvBeforeTax(dto.getNpvBeforeTax());
        entity.setNpvAfterTax(dto.getNpvAfterTax());
        entity.setCutoffYear(dto.getCutoffYear());
        return entity;
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ModelKeyItemRefVO queryById(String id) {
        ModelKeyItemRef entity = this.modelKeyItemRefDAO.selectById(id);
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }

    /**
     * 查询多条数据
     *
     * @param dto 数据传递类实例对象
     * @return 对象列表
     */
    @Override
    public List<ModelKeyItemRefVO> queryList(ModelKeyItemRefDTO dto) {
        //mybatis-plus 写法示例
        //IPage<ModelKeyItemRef> pageResult = this.evaModelDAO.selectPage(new Page<>(dto.getPageNo(),dto.getPageSize()),null);
        //List<ModelKeyItemRef> dataList = pageResult.getRecords();
        //dto.setCount((int) pageResult.getTotal());
        List<ModelKeyItemRef> dataList = this.modelKeyItemRefDAO.queryList(dto);
        if (null == dataList || dataList.isEmpty()) {
            return Collections.emptyList();
        }
        List<ModelKeyItemRefVO> resultList = new ArrayList(dataList.size());
        for (ModelKeyItemRef item : dataList) {
            ModelKeyItemRefVO vo = parseEntity2VO(item);
            resultList.add(vo);
        }
        // TODO 添加其他关联查询数据
        return resultList;
    }

    /**
     * 新增数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult create(ModelKeyItemRefDTO dto) {
        // TODO 添加校验逻辑
        ModelKeyItemRef entity = parseDTO2Entity(dto);
        preInsert(entity);
        this.modelKeyItemRefDAO.insert(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 修改数据
     *
     * @param dto 实例对象
     * @return 操作结果
     */
    @Override
    public ServiceResult update(ModelKeyItemRefDTO dto) {
        String id = dto.getId();
        if (StringUtils.isEmpty(id)) {
            return CommonResultEnum.NOT_EXIST;
        }
        ModelKeyItemRef entity = this.modelKeyItemRefDAO.selectById(id);
        if (entity == null) {
            return CommonResultEnum.NOT_EXIST;
        }
        // TODO 添加其他校验逻辑，如数据权限

        entity.setModelId(dto.getModelId());
        entity.setIrrBeforeTax(dto.getIrrBeforeTax());
        entity.setIrrAfterTax(dto.getIrrAfterTax());
        entity.setPbpBeforeTax(dto.getPbpBeforeTax());
        entity.setPbpAfterTax(dto.getPbpAfterTax());
        entity.setNpvBeforeTax(dto.getNpvBeforeTax());
        entity.setNpvAfterTax(dto.getNpvAfterTax());
        entity.setCutoffYear(dto.getCutoffYear());
        this.modelKeyItemRefDAO.updateById(entity);
        return CommonResultEnum.SUCCESS;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 操作结果
     */
    @Override
    public ServiceResult deleteById(String id) {
        // TODO 添加校验逻辑，如数据权限
        this.modelKeyItemRefDAO.deleteById(id);
        return CommonResultEnum.SUCCESS;
    }

    @Override
    public ModelKeyItemRefVO queryByModel(String modelId) {
        ModelKeyItemRef entity = this.modelKeyItemRefDAO.selectOne(new QueryWrapper<ModelKeyItemRef>()
                .lambda().eq(ModelKeyItemRef::getModelId,modelId));
        if (entity == null) {
            return null;
        }
        return parseEntity2VO(entity);
    }
}
